#include <iostream>
#include <vector>

using namespace std;

vector<int> printMatrix(vector<vector<int> > matrix) {
	vector<int> printSeq;
	int rows = matrix.size();
	if (rows == 0) return printSeq;
	if (rows == 1) return matrix[0];
	int cols = matrix[0].size();
	if (cols == 1)
	{
		for (int i = 0; i < rows; ++i)
			printSeq.push_back(matrix[i][0]);
		return printSeq;
	}
	int clc = ((rows < cols ? rows : cols) - 1) / 2 + 1;
	for(int cnt = 0; cnt < clc; ++cnt)
	{
		for (int i = cnt; i < cols - cnt -1; ++i)
		{
			printSeq.push_back(matrix[cnt][i]);
		}
		for (int i = cnt; i < (rows - cnt - 1); ++i)
		{
			printSeq.push_back(matrix[i][cols - cnt - 1]);
		}
		for (int i = (cols - cnt - 1); i > cnt; --i)
		{
			printSeq.push_back(matrix[rows - cnt - 1][i]);
		}
		for (int i = (rows - cnt - 1); i > cnt; --i)
		{
			printSeq.push_back(matrix[i][cnt]);
		}
	}
	/*if (rows == cols && rows % 2)
		printSeq.push_back(matrix[rows / 2][cols / 2]);*/
	return printSeq;
}


vector<int> printMatrix2(vector<vector<int> > matrix) {
	vector<int> printSeq;
	int rows = matrix.size();
	//if (rows == 1) return matrix[0];
	int cols = matrix[0].size();
	if (rows == 0 || cols == 0) return printSeq;
	int left = 0, top = 0, right = cols - 1, buttom = rows - 1;
	while (left <= right && top <= buttom)
	{
		for (int i = left; i <= right; ++i)
			printSeq.push_back(matrix[top][i]);
		for (int i = top + 1; i <= buttom; ++i)
			printSeq.push_back(matrix[i][right]);
		if(top != buttom)
			for (int i = right - 1; i >= left; --i)
				printSeq.push_back(matrix[buttom][i]);
		if(left != right)
			for (int i = buttom - 1; i > top; --i)
				printSeq.push_back(matrix[i][left]);
		++left;
		++top;
		--right;
		--buttom;
	}
	return printSeq;
}

//int main()
//{
//	int num(1);
//	vector<vector<int> > v;
//	for (int i = 0; i < 4; ++i)
//	{
//		vector<int> row;
//		for (int j = 0; j < 4; ++j)
//		{
//			row.push_back(num);
//			++num;
//		}
//		v.push_back(row);
//	}
//
//	vector<int> p = printMatrix2(v);
//
//	cout << p.size() << endl;
//	for (auto i : p)
//		cout << i << endl;
//
//	system("pause");
//	return 0;
//}